package com.example.demo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
public class Controller1 {
    private static AtomicInteger baseLatency = new AtomicInteger(0);
    Logger logger = LoggerFactory.getLogger(Controller1.class);

    @GetMapping("/test1")
    public Object test1() throws InterruptedException {
        logger.debug("request to test1");
        Thread.sleep(baseLatency.get() + new Random().nextInt(200));
        return LocalDateTime.now();
    }

    @PostMapping("/test2")
    public Object test2() throws InterruptedException {
        logger.debug("request to test2");
        Thread.sleep(baseLatency.get()/2 + new Random().nextInt(20));
        return LocalDateTime.now();
    }

    @Scheduled(fixedRate = 10000)
    public void updateBaseLatency() {
        int newValue = new Random().nextInt(300);
        logger.info("update base latency to {}", newValue);
        baseLatency.set(newValue);
    }
}
